package belf.migrate.engine.ddl.type.sqlserver;

import belf.migrate.api.model.QualifiersModel;
import belf.migrate.api.table.schema.Catalog;
import belf.migrate.engine.ddl.BaseDDLConvert;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SqlServer2KingbaseTypeConvert extends BaseDDLConvert {
    public SqlServer2KingbaseTypeConvert(String objectName, String sql, Catalog sourceCatalog,
                                         Catalog sinkCatalog, String schema,
                                         QualifiersModel qualifiersModel) {
        super(objectName, sql, sourceCatalog, sinkCatalog, schema, qualifiersModel);
    }

    @Override
    public String convert() {
        // FIXME: 目前SqlServer的自定义类型到金仓的转换，替换FROM为AS，替换TABLE为OBJECT，后续再完善
        // 基于SQLServer的基础数据类型创建用户定义的数据类型（User-Defined Data Types, UDT）
        // 直接替换FROM==>AS
        Pattern pattern1 = Pattern.compile("\\sFROM\\s", Pattern.CASE_INSENSITIVE);
        Matcher matcher1 = pattern1.matcher(sql);
        String result = matcher1.replaceAll("AS");
        // 用户定义的表类型（User-Defined Table Types），可以用于存储过程或函数中作为表变量或表值参数。
        // 直接替换TABLE==>OBJECT
        Pattern pattern2 = Pattern.compile("\\sTABLE\\s", Pattern.CASE_INSENSITIVE);
        Matcher matcher2 = pattern2.matcher(result);
        result = matcher2.replaceAll("OBJECT");
        return result;
    }
}
